package com.yinhai.mycat_test.controller;

import com.yinhai.mycat_test.entity.IDCard;
import com.yinhai.mycat_test.service.read.IDCardReadService;
import com.yinhai.mycat_test.service.read.UserReadService;
import com.yinhai.mycat_test.service.write.IDCardWriteService;
import com.yinhai.mycat_test.service.write.UserWriteService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/id")
public class IDController {

    @Resource
    IDCardWriteService idCardWriteService;

    @Resource
    IDCardReadService idCardReadService;

    @Resource
    UserWriteService userWriteService;

    @Resource
    UserReadService userReadService;

    @PostMapping("/insertIDCard")
    public void insertIDCard() {
        IDCard idCard = new IDCard();
        idCard.setUserId(4L);
        idCard.setId(4L);
        idCard.setExpireDate(System.currentTimeMillis());
        idCard.setAddress("hebei");
        idCard.setIdCard("123456789");
        idCardWriteService.insertIDCard(idCard);
    }

    @PostMapping("/deleteAllIDCard")
    void deleteAllIDCard() {
        idCardWriteService.deleteAllIDCard();
    }

    @PostMapping("/batchDeleteIDCardById")
    void batchDeleteIDCardById() {
        List<Long> ids = new ArrayList<>();
        //用户ID 并且删除IDCARD
        for (int i = 1; i < 5000; i++) {
            if (i % 6 == 0) {
                IDCard idCard = idCardReadService.queryIDCardById(Long.valueOf(i));
                if (idCard != null) {
                    ids.add(idCard.getId());
                }
            }
        }


        idCardWriteService.batchDeleteIDCardById(ids);
    }

    @PostMapping("/batchInsertIDCard")
    void batchInsertIDCard() {
        List<IDCard> list = new ArrayList<>();
        IDCard idCard = null;
        for (long i = 1; i < 10000; i++) {
            idCard = new IDCard();
            idCard.setUserId(i);
            if(i%4 ==0) {
                idCard.setAddress("hebei");
            }else if(i%4 ==1){
                idCard.setAddress("beijing");
            }else if(i%4 ==2){
                idCard.setAddress("chengdu");
            }else {
                idCard.setAddress("other");
            }
            idCard.setId(i);
            idCard.setExpireDate(System.currentTimeMillis()+100000);
            idCard.setIdCard("1");
            list.add(idCard);
        }
        idCardWriteService.batchInsertIDCard(list);
    }

    @PostMapping("/deleteIDCardById")
    void deleteIDCardById() {
        idCardWriteService.deleteIDCardById(3L);
    }

    /**
     * 查询所有用户
     * */
    @GetMapping("/queryAllIDCard")
    List<IDCard> queryAllIDCard(){
       return idCardReadService.queryAllIDCard();
    }

    /**
     * 根据性别查询用户
     * */
    @GetMapping("/queryIDCardById")
    IDCard queryIDCardById(){
        return idCardReadService.queryIDCardById(2475L);
    }

    /**
     * 根据用户姓名模糊查询用户
     * */
    @GetMapping("/queryIDCardLike02")
    List<IDCard> queryIDCardLike02(){
        return idCardReadService.queryIDCardLike02("bei");
    }
    /**
     * 查询用户数量
     * */
    @GetMapping("/queryIDCardCount")
    Integer queryIDCardCount(){
        return idCardReadService.queryIDCardCount();
    }
}
